// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/testing/gtest_utils.h>
#include <pollux/functions/dynamic_library_loader.h>
#include <pollux/expression/simple_function_registry.h>
#include <pollux/functions/function_registry.h>
#include <pollux/testing/functions/prestosql/function_base_test.h>
#include <pollux/testing/vector/vector_test_base.h>

using namespace kumo::pollux::test;

namespace kumo::pollux::functions::test {
    std::string getLibraryPath(const std::string &filename) {
        return fmt::format(
            "{}/{}{}",
            POLLUX_TEST_DYNAMIC_LIBRARY_PATH,
            filename,
            POLLUX_TEST_DYNAMIC_LIBRARY_PATH_SUFFIX);
    }


    void load() {
        auto signaturesBefore = get_function_signatures().size();
        std::string libraryPath = getLibraryPath("libpollux_function_dynamic");
        auto rs = load_dynamic_library(libraryPath);
        EXPECT_EQ(rs.ok(), true) << rs.to_string();
        auto signaturesAfter = get_function_signatures().size();
        EXPECT_EQ(signaturesAfter, signaturesBefore + 1);
        KLOG(INFO)<<"simple_functions():"<<&exec::simple_functions();

        auto &registry = exec::simple_functions();
        auto fns = registry.get_function_names();
        KLOG(INFO)<<"functions:"<<fns.size();
        for (const auto &fn : fns) {
            KLOG(INFO)<<fn;
        }
        auto resolved = registry.resolve_function("dynamic", {});
        EXPECT_EQ(TypeKind::BIGINT, resolved->type()->kind());
    }
}

int main(int argc, char **argv) {
    kumo::pollux::functions::test::load();
    return 0;
}
